package cn.matezk.cloud.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("message")
@Slf4j
public class MessageController {
    @Value("${wecaht.appId}")
    private String appId;

    @PostMapping("send")
    public JSONObject send(@RequestBody JSONObject message) {
        log.info(message.toString());
        HttpResponse response = new HttpRequest(String.format("https://api.weixin.qq.com/cgi-bin/message/template/send?from_appid=%s", appId))
                .method(Method.POST).body(message.toString()).execute();
        log.info("%d: %s", response.getStatus(), response.body());
        return JSONUtil.parseObj(response.body());
    }
}
